#include "stdafx.h"
#include "../vfs/VirtualLocation.h"

/*
class vfs::VirtualLocation::VFileIterator : public vfs::VirtualLocation::Iterator::IImplementation
{
	friend class vfs::VirtualLocation;
	typedef vfs::VirtualLocation::Iterator::IImplementation tBaseClass;

	VFileIterator(vfs::VirtualLocation* pLoc): tBaseClass(), m_pLoc(pLoc)
	{
		THROWIFFALSE(pLoc, L"");
		_vfile_iter = m_pLoc->m_FileMap.begin();
	}
public:
	VFileIterator() : tBaseClass(), m_pLoc(NULL)
	{};
	virtual ~VFileIterator()
	{};
	virtual vfs::File*	value()
	{
		if(m_pLoc && _vfile_iter != m_pLoc->m_FileMap.end())
		{
			return _vfile_iter->second;
		}
		return NULL;
	}
	virtual void				next()
	{
		if(m_pLoc && _vfile_iter != m_pLoc->m_FileMap.end())
		{
			_vfile_iter++;
		}
	}
protected:
	virtual tBaseClass* clone()
	{
		VFileIterator* iter = new VFileIterator(m_pLoc);
		iter->_vfile_iter = _vfile_iter;
		return iter;
	}
private:
	vfs::VirtualLocation*						m_pLoc;
	vfs::VirtualLocation::tVFiles::iterator	_vfile_iter;
};
*/

//------------------------------//

namespace ja2{ namespace vfs
{
	VirtualLocation::VirtualLocation(boost::filesystem::path const &Path, bool IsExclusive)
		: m_Path(Path), m_Exclusive(IsExclusive)
	{
	}

	bool VirtualLocation::file_exist(boost::filesystem::path const &File) const
	{
		return (m_FileMap.count(File) == 1);
	}

	FileProxy VirtualLocation::get_file(boost::filesystem::path const &FileName) const
	{
		return m_FileMap.find(FileName)->second;
	}

	void VirtualLocation::add_file(FileProxy const &File)
	{
		m_FileMap.insert(std::make_pair(File.get_path().filename(), File));
	}
} /*vfs*/} /*ja2*/
/*
void vfs::VirtualLocation::setIsExclusive(bool exclusive)
{
	m_exclusive = exclusive;
}
bool vfs::VirtualLocation::getIsExclusive()
{
	return m_exclusive;
}

void vfs::VirtualLocation::addFile(vfs::BaseFile* file, utf8string const& profileName)
{
	tVFiles::iterator it = m_FileMap.find(file->getName());
	vfs::File *pVFile = NULL;
	if(it == m_FileMap.end())
	{
		vfs::Path fp = file->getPath();
		vfs::CProfileStack& stack = *(getVFS()->getProfileStack());
		pVFile = vfs::File::create(fp,stack);
		it = m_FileMap.insert(m_FileMap.end(), std::pair<vfs::Path,vfs::File*>(file->getName(),pVFile));
	}
	it->second->add(file,profileName,true);
}

vfs::BaseFile* vfs::VirtualLocation::getFile(vfs::Path const& filename, utf8string const& profileName) const
{
	tVFiles::const_iterator cit = m_FileMap.find(filename);
	if(cit != m_FileMap.end() && cit->second)
	{
		if(profileName.empty())
		{
			if(m_exclusive)
			{
				return cit->second->file(vfs::File::SF_STOP_ON_WRITABLE_PROFILE);
			}
			else
			{
				return cit->second->file(vfs::File::SF_TOP);
			}
		}
		else
		{
			// you know what you are doing
			return cit->second->file(profileName);
		}
	}
	return NULL;
}
vfs::File* vfs::VirtualLocation::getVirtualFile(vfs::Path const& filename)
{
	tVFiles::const_iterator cit = m_FileMap.find(filename);
	if(cit != m_FileMap.end())
	{
		return cit->second;
	}
	return NULL;
}

bool vfs::VirtualLocation::removeFile(vfs::BaseFile* file)
{
	if(file)
	{
		vfs::Path sDir,sFile;
		file->getPath().splitLast(sDir,sFile);
		tVFiles::iterator it = m_FileMap.find(sFile);
		if(it != m_FileMap.end())
		{
			if(!it->second->remove(file))
			{
				//CVirtualFile* vfile = it->second;
				//delete vfile;
				m_FileMap.erase(it);
			}
			return true;
		}
	}
	return false;
}


vfs::VirtualLocation::Iterator vfs::VirtualLocation::iterate()
{
	return Iterator(new VFileIterator(this));
}

*/